const app = getApp()
Page({
  data: {
    isCheck: -1,
    addressList: [
    ],
    configState: true,
    dialogBodyStyle: {
      width: '80%'
    },
    enter: ''
  },
  onLoad: function (options) {
    if (JSON.stringify(options) != "{}") {
      this.setData({
        enter: options.enter
      })
    }

  },
  onShow: function () {
    this.getAddress()
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.getAddress()
    setTimeout(() => {
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 100);
  },
  setDefaultAddress(e) {
    let id = e.currentTarget.dataset.id;

    this.data.addressList.map((item) => {
      if (item.id === id) {
        item.status = 1
      } else {
        item.status = 0
      }
    })
    this.setData({
      addressList: this.data.addressList
    })
  },
  editAddress(e) {
    let item = e.currentTarget.dataset.item;
    let type = e.currentTarget.dataset.type;
    if (type !== 'delete' && this.data.enter !== 'order') {
      app.WxService.navigateTo('/pages/address-edit/index', item)
    } else {
      app.goBack({ addressId: item.id }, 2, 1)
    }
  },
  deleteAddress(e) {
    let _this = this
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除该地址吗？',
      success(res) {
        if (res.confirm) {
          app.Http.http({
            url: app.Api.AddressDelete,
            method: 'POST',
            params: {
              id: Number(id)
            }
          }).then(data => {
            _this.getAddress()
          }).catch(err => {
            console.log(err)

          })
        }
      }
    })

  },
  getAddress() {
    app.Http.http({
      url: app.Api.AddressList,
      method: 'POST',
      params: {}
    }).then(data => {
      this.setData({
        addressList: data
      })

    }).catch(err => {
      console.log(err)
    })
  },
  addAddress() {
    app.WxService.navigateTo('/pages/address-edit/index')
  },
  changeData(data) {

  }
})